/*
 * 品牌列表
 * @Author: ljb
 * @Date: 2018-03-27 17:53:05
 * @Last Modified by: kanglin
 * @Last Modified time: 2018-06-01 10:47:04
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

</style>
<template>
	<div class="v-customer-product-material-index">

		<Table
			:columns="columns"
			:loading="loading"
			:data="assets"
			:height="tableHeight()"
			class="personal-scrollbar"
			@on-sort-change="sortable"/>

	</div>
</template>

<script>

import Button from 'iview/src/components/button';
import { computedTableHeight } from 'src/views/mixins/computedTableHeight';

export default {
	name: 'MaterialIndex',
	mixins: [computedTableHeight],
	props: {
		loading: {
			type: Boolean,
			default() {
				return false;
			},
		},
		categories: {
			type: Array,
			default() {
				return [];
			},
		},
		assets: {
			type: Array,
			default() {
				return [];
			},
		},
	},
	data() {
		return {
			columns: [
				{
					title: this.$t('tryon_material.category'),
					key: 'category_id',
					align: 'center',
					render: (h, { row }) => <div> { this.showCategoryName(row.category_id) } </div>,
				},
				{
					title: this.$t('tryon_material.code'),
					key: 'code',
					align: 'center',
					render: (h, { row }) => <div> { row.code || '-' } </div>,
				},
				{
					title: '创建时间',
					key: 'created_at',
					align: 'center',
					// sortable: 'custom',
					render: (h, { row }) => <div> { !row.created_at ? '-' : row.created_at } </div>,
				},
				{
					title: '操作',
					align: 'center',
					width: 200,
					render: (h, { row }) => (
						<div>
							<Button size="small" style={{ marginRight: '5px' }} on-click={() => { this.$emit('edit', row); }} type="primary">{ this.$t('common.edit') }</Button>
							<Button size="small" on-click={() => { this.$emit('delete', row.id); }} type="error">{ this.$t('common.delete') }</Button>
						</div>),
				},
			],
		};
	},
	methods: {

		/**
         * 排序的回调
         * @param  {Object} param {对应列的属性对象,里面包含了column列属性，key值，order排序字段}
         * @return {undefined}
         */
		sortable(param) {
			const search_param = {

				order_by: param.key,

				sort: param.order === 'normal' ? '' : param.order,

			};

			this.$emit('sort', search_param);
		},

		/**
         * 显示分类名称
         * @param  {String|Number} id {分类id}
         * @return {String} {分类名称}
         */
		showCategoryName(id) {
			const [current_category] = this.categories.filter(category => parseInt(category.id, 10) === parseInt(id, 10));

			return current_category ? current_category.name : '-';
		},

	},
};
</script>
